<template>
	<view class="lottie-bg">
		<view id="lottie">
			<image :src="loadingImages" rel="preload" mode="widthFix" style="width: 100%" />
		</view>
		<view class="Skip-over" @click="SkipOver">
			{{count}} 跳过
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapGetters,
		mapMutations,
		mapActions
	} from "vuex";
	import {
		wxappAuth,
		updateSpread
	} from "@/api/user";
	import {
		getLoadigImages,
		getSystemConfigData
	} from "@/api/public";
	import dayjs from "dayjs";
	import store from "@/store";
	import cookie from "@/utils/store/cookie";
	import {
		parseQuery,
		login,
		handleQrCode,
		getCurrentPageUrl,
		handleUrlParam,
		getCurrentPageUrlWithArgs
	} from "@/utils";

	export default {
		name: "Loading",
		data() {
			return {
				count: '',
				loadingImages: ''
			};
		},
		computed: mapGetters(["userInfo"]),
		onShow() {
			this.getLoadigImages()

			this.$store.dispatch('getSystemConfigData')
		},
		methods: {
			...mapActions(["changeAuthorization", "setUserInfo", "getUser"]),
			toLaunch() {
				this.changeAuthorization(false);
				let redirect = cookie.get('redirect').replace(/\ /g, '')
				if (redirect && redirect.indexOf('/pages') != -1) {
					this.$yrouter.replace({
						path: '/pages' + redirect.split('/pages')[1],
					});
					cookie.remove('redirect');
				} else {
					this.$yrouter.switchTab({
						path: "/pages/home/index"
					});
				}
			},
			SkipOver(){
				clearInterval(this.timer);
				this.toIndex()
			},
			toIndex(){
				console.log('getUser')
				var url = handleQrCode();
				if (url) {
					let urlSpread = parseInt(url.spread);
					if (urlSpread) {
						cookie.set("spread", urlSpread);
					}
				}
				if (this.$deviceType == "app" || this.$deviceType == "weixinh5") {
					this.$yrouter.switchTab({
						path: "/pages/home/index"
					});
					return;
				}
				let spread = cookie.get('spread');
				console.log(this.userInfo.isNoSpread)
				this.$store.dispatch('getUser').then(res => {
					if (this.userInfo.spreadUid == 0) {
						if (this.userInfo.isNoSpread != 'Y') {
							updateSpread(spread).then(() => {}).catch(() => {});
						}
					}
				}).catch( err => {
				    
				})
				if (this.$store.getters.token) {
					// 如果token存在，直接进行进页面
					console.log('登录状态存在，直接进页面')
					this.toLaunch();
					return;
				}
				login().finally(() => {
					this.$yrouter.switchTab({
						path: "/pages/home/index"
					});
				});
			},
			countDown(){
				const TIME_COUNT = 3;
				if(!this.timer){
					this.count = TIME_COUNT;
					this.timer = setInterval(()=>{
					if(this.count > 0 && this.count <= TIME_COUNT){
						this.count--;
					}else{
						clearInterval(this.timer);
						this.timer = null;
						this.toIndex()
					}
				  },1000)
				}
			},
			getLoadigImages(){
				let that = this
				getLoadigImages().then((res) => {
						that.loadingImages = JSON.parse(JSON.stringify(res.data))
						this.countDown()
					})
					.catch(err => {
				
					})
			}
		}
	};
</script>

<style scoped lang="less">
	.lottie-bg {
		position: fixed;
		left: 0;
		top: 0;
		background-color: #fff;
		width: 100%;
		height: 100%;
		z-index: 999;
		display: -webkit-flex;
		display: flex;
		-webkit-align-items: center;
		align-items: center;
		-webkit-justify-content: center;
		justify-content: center;
	}

	#lottie {
		width: 100%;
		display: block;
		overflow: hidden;
		transform: translate3d(0, 0, 0);
		margin: auto;
	}
	.Skip-over{
		position: fixed;
		bottom: 50rpx;
		right: 40rpx;
		height: 50rpx;
		width: auto;
		padding: 0 30rpx;
		display: inline-block;
		border-radius: 50rpx;
		z-index: 100;
		background-color: rgba(255,255,255,0.4);
		line-height: 50rpx;
	}
</style>
